package com.ys.utils.hjzf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ys.base.BaseParam;
import com.ys.utils.alipay.model.MsgInfo;
import com.ys.utils.base.j2se.DateHandler;
import com.ys.utils.base.j2se.Logger;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.hjzf.sdk.entity.RequestParam;
import com.ys.utils.hjzf.sdk.enums.SignTypeEnum;
import com.ys.utils.hjzf.sdk.utils.*;
import com.ys.utils.mobao.Mobo360Config;
import com.ys.utils.spring.config.SystemError;
import com.ys.web.hjzf.model.TransNotify;
import com.ys.web.mobao.model.po.BindCard;
import com.ys.web.pay.model.PayOrder;
import com.ys.web.pay.model.WithdrawRecordWait;

import java.lang.reflect.Field;
import java.util.*;

public class HjzfHandler {

//    public static String web_url = "https://api.ys168.vip";
    //商户号
    public static String mch_no = "888120100006610";
    //MD5私钥
    public static String secretKey = "f98d922300694c0c9634bdab8a38e20e";
    //RSA私钥
    public static String privateKey= "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJHmVEfk7to8zB5CVleZjDiu8YLVld5cz58TEd/O7bI4sLq4H5jSZwOzyI2vpBtE3YxLT53j9JXYlXUyavZyYep12yqUMWv56BsvrLtYVNK6ZW9XdOshp6SQiniRZbMMXw9LpHyynZYniw4U44HAdC3Yp5ytoYh7NAkdCtZJnPGhAgMBAAECgYAycjVJ9novsz3QaJd6CnlVUAfpePgsvmJ7WhigDehZ6wgQR4GFUZqUySDa/JP9ab5At/l7WkpDVXO35grd04bPCJoSHXSuOYFEdOGTkEkbH8TfLHJZph6kL5Ifmd6CwC2GVw8QVMlWdsQX0A0xdFtXJMS2cLIxkK9K0MeZYdW7EQJBAM/CNV8s7fRPiNKl8FAe4lga+kfGVjrrafr4wbeQh/5LFkzFzcsXLMrkKnQXzVvGgo1LHcNjJYpa7hr8yKOXLY8CQQCzxwsVlD6Yjw9J7vkaxrA/8llj5gF8q+86WaSjt3i0sIbW85FaG4BFnxZf5FDpwNXRFFYu4TmPRhBMF/h7x7XPAkAPttPMoBxbd0yVCkIT/VIHcfruRN0u1bGNbxVP6hmvLUTBh9CaBOW5buqbH1XEFE65my3kInKrdVc/KVGPthaXAkBnv52Q9X1uU1kSZLwTjMhYDwcxVKJVDXBAr00JQi+Br0IRuHyQkMBk7VtfOYbMb4G1aFQYN9gc6+Up1MeMv4MXAkBAUWlKH8loquAAWRbfhjaavprgByQD1LiL6/kd2BuUrBDuZp8NFkSNLKl08lCLmFr3YoVUf2J+Focl1WLEPJIY";
    //RSA公钥（支付时用） 商户中心->新密钥管理->密钥管理获取
    public static String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCF0mQlH7BHfGT2qw7D7Eb/uLO8GddT5MLKMsavSPomCwc1tt7FZHgTpIF6xkq4o0K7OBDETtitEpUlJscFQiTfltXHop90HwRVyRuFxZC0jcQpWkjG8foJvyVpmt8bVa1SNbm67YYXyWfEb83kFXfhKiy4icdF4H1pfp31du1tNQIDAQAB";//公钥Str
    //RSA公钥（转账时用）商户中心->商户信息->密钥管理获取
    public static String publicTransKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJ/zUjv5keJsUYSjfyPYWQeZYjIMtFC0m4YhofXQd/a2ETQq/BXoyhjKAxlk8eN+ppUhGRIu9a1N2kyQ4GBFd4UqWpFO1XCRh3F/kKI94QDDjAUghXTHB0y9hW05ChhS00YyfQR9v3LOD8UXpOCcCQ2/uATrhDvWkaT7sFapcsGQIDAQAB";//公钥Str
    //RSA密钥原文
    public static String publicKeySign = "1234567891234567";//密钥原文


    /**
     * 绑定银行卡发送短信
     * @param bindCard
     * @return
     */
    public static MsgInfo signSms(BindCard bindCard){
        MsgInfo msgInfo = new MsgInfo();
        try {
            Map<String, Object> map = new HashMap<>();
            RequestParam requestParam = new RequestParam();
            requestParam.setMch_no(mch_no);// 商户编号
            requestParam.setMethod("fastPay.agreement.signSms");// 固定方法名
            requestParam.setVersion("1.0");// 版本号
            requestParam.setRand_str(getUUID());// 随机字符串
            requestParam.setSign_type("2");// 签名类型
            requestParam.setSec_key(RSAUtil.encryptByPublicKey(publicKeyStr,publicKeySign));//加密密钥
            map.put("mch_order_no", bindCard.cardToken); // 商户订单号
            map.put("order_amount", "0.01"); // 订单金额
            map.put("mch_req_time", DateHandler.getDate(new Date(),"yyyy-MM-dd HH:mm:ss")); // 下单时间
            map.put("payer_name",AESUtil.Aes256Encode(bindCard.userName,"1234567891234567")); // 姓名
            map.put("id_type", "1"); // 证件类型
            map.put("id_no", AESUtil.Aes256Encode(bindCard.idCardNum,"1234567891234567")); // 证件号码
            map.put("bank_card_no", AESUtil.Aes256Encode(bindCard.cardNo,"1234567891234567")); // 银行卡号
            map.put("mobile_no", AESUtil.Aes256Encode(bindCard.mobile, "1234567891234567")); // 手机号
            requestParam.setData(JsonUtil.toString(map));
            String signStr = getSortedString(requestParam);
            // 签名
            if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) {
                signStr =(RSAUtil.sign(signStr, privateKey, false));
            } else {
                signStr = "";
            }
            if(StringHandler.isEmpty(signStr)){
                msgInfo.isSuccess = false;
                return msgInfo;
            }
            requestParam.setSign(signStr);
            // Map转json字符串
            String reqBodyJson = JSON.toJSONString(requestParam);
            String httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson);
            Logger.info("绑卡短信返回信息-------"+httpResponseJson);
            Map<String, Object> httpResponseMap = (Map<String, Object>) JSONObject.parse(httpResponseJson);
            if(StringHandler.isNotEmpty(httpResponseMap)) {
                msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("message"));
                String resp_code = StringHandler.getString(httpResponseMap.get("resp_code"));
                String biz_code = StringHandler.getString(httpResponseMap.get("biz_code"));
                if(resp_code.equals("SUCCESS") && biz_code.equals("JS000000")){
                    Map<String, Object> dataMap = JsonUtil.toBean(httpResponseMap.get("data")+"",Map.class);
                    if(dataMap.get("order_status").equals("P1000")){
                        msgInfo.isSuccess = true;
                    }else if(dataMap.get("order_status").equals("P3000")){
                        msgInfo.isSuccess = true;
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("err_msg"));
                    }else{
                        msgInfo.isSuccess = false;
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("err_msg"));
                    }
                }else{
                    if(StringHandler.isNotEmpty(httpResponseMap.get("data"))) {
                        Map<String, Object> dataMap = JsonUtil.toBean(httpResponseMap.get("data")+"",Map.class);
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("errorDesc"));
                    }else{
                        msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("biz_msg"));
                    }
                    msgInfo.isSuccess=false;
                    Logger.error("绑卡失败--"+msgInfo.errMsg);
                }
            }
        }catch (Exception e){
            Logger.error(e);
            SystemError.wrapBs("500","绑卡失败");
        }
        return msgInfo;
    }

    public static MsgInfo smsSign(String orderNo,String smsCode){
        MsgInfo msgInfo = new MsgInfo();
        try {
            Map<String, Object> map = new HashMap<>();
            RequestParam requestParam = new RequestParam();
            requestParam.setMch_no(mch_no);// 商户编号
            requestParam.setMethod("fastPay.agreement.smsSign");// 固定方法名
            requestParam.setVersion("1.0");// 版本号
            requestParam.setRand_str(getUUID());// 随机字符串
            requestParam.setSign_type("2");// 签名类型
            requestParam.setSec_key(RSAUtil.encryptByPublicKey(publicKeyStr,publicKeySign));//加密密钥
            map.put("mch_order_no", orderNo); // 商户订单号
            map.put("sms_code", smsCode); // 签约短信验证码
            requestParam.setData(JsonUtil.toString(map));
            String signStr = getSortedString(requestParam);
            // 签名
            if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) {
                signStr = (RSAUtil.sign(signStr, privateKey, false));
            } else {
                signStr = "";
            }
            if(StringHandler.isEmpty(signStr)){
                msgInfo.isSuccess = false;
                return msgInfo;
            }
            requestParam.setSign(signStr);
            // Map转json字符串
            String reqBodyJson = JSON.toJSONString(requestParam);
            String httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson);
            Logger.info("绑卡短信签约返回信息-------"+httpResponseJson);
            Map<String, Object> httpResponseMap = JsonUtil.toBean(httpResponseJson,Map.class);
            if(StringHandler.isNotEmpty(httpResponseMap)) {
                msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("message"));
                String resp_code = StringHandler.getString(httpResponseMap.get("resp_code"));
                String biz_code = StringHandler.getString(httpResponseMap.get("biz_code"));
                if(resp_code.equals("SUCCESS") && biz_code.equals("JS000000")){
                    Map<String, Object> dataMap = JsonUtil.toBean(httpResponseMap.get("data")+"",Map.class);
                    if(dataMap.get("order_status").equals("P1000")){
                        msgInfo.thirdOrder = StringHandler.getString(dataMap.get("sign_no"));
                        msgInfo.payFundOrderId = StringHandler.getString(dataMap.get("bank_code"));
                        msgInfo.isSuccess = true;
                    }else if(dataMap.get("order_status").equals("P3000")){
                        msgInfo.isSuccess = true;
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("err_msg"));
                    }else{
                        msgInfo.isSuccess = false;
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("err_msg"));
                    }
                }else{
                    if(StringHandler.isNotEmpty(httpResponseMap.get("data"))) {
                        Map<String, Object> dataMap = JsonUtil.toBean(httpResponseMap.get("data")+"",Map.class);
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("errorDesc"));
                    }else{
                        msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("biz_msg"));
                    }
                    msgInfo.isSuccess=false;
                    Logger.error("绑卡失败--"+msgInfo.errMsg);
                }
            }
        }catch (Exception e){
            Logger.error(e);
            msgInfo.isSuccess = false;
        }
        return msgInfo;
    }

    public static MsgInfo unSign(String sign_no,int type){
        MsgInfo msgInfo = new MsgInfo();
        try {
            Map<String, Object> map = new HashMap<>();
            RequestParam requestParam = new RequestParam();
            requestParam.setMch_no(mch_no);// 商户编号
            requestParam.setMethod("fastPay.agreement.unSign");// 固定方法名
            requestParam.setVersion("1.0");// 版本号
            requestParam.setRand_str(getUUID());// 随机字符串
            requestParam.setSign_type("2");// 签名类型
            requestParam.setSec_key(RSAUtil.encryptByPublicKey(publicKeyStr,publicKeySign));//加密密钥
            map.put("mch_order_no", BaseParam.getOrderSN()); // 商户订单号
            map.put("mch_req_time", DateHandler.getDate(new Date(),"yyyy-MM-dd HH:mm:ss")); // 订单时间
            if(type==1){
                map.put("sign_no",  AESUtil.Aes256Encode(sign_no,"1234567891234567")); // 签约银行卡号
            }else if(type==2){
                map.put("bank_card_no",  AESUtil.Aes256Encode(sign_no,"1234567891234567")); // 签约银行卡号
            }
            requestParam.setData(JsonUtil.toString(map));
            String signStr = getSortedString(requestParam);
            // 签名
            if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) {
                signStr = (RSAUtil.sign(signStr, privateKey, false));
            } else {
                signStr = "";
            }
            if(StringHandler.isEmpty(signStr)){
                msgInfo.isSuccess = false;
                return msgInfo;
            }
            requestParam.setSign(signStr);
            // Map转json字符串
            String reqBodyJson = JSON.toJSONString(requestParam);
            String httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson);
            Map<String, Object> httpResponseMap = (Map<String, Object>) JSONObject.parse(httpResponseJson);
            if(StringHandler.isNotEmpty(httpResponseMap)) {
                msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("message"));
                String resp_code = StringHandler.getString(httpResponseMap.get("resp_code"));
                String biz_code = StringHandler.getString(httpResponseMap.get("biz_code"));
                if(resp_code.equals("SUCCESS") && biz_code.equals("JS000000")){
                    msgInfo.isSuccess = true;
                }else{
                    if(StringHandler.isNotEmpty(httpResponseMap.get("data"))) {
                        Map<String, Object> dataMap = JsonUtil.toBean(httpResponseMap.get("data")+"",Map.class);
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("errorDesc"));
                    }else{
                        msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("biz_msg"));
                    }
                    msgInfo.isSuccess=false;
                    Logger.error("解绑失败--"+msgInfo.errMsg);
                }
            }
        }catch (Exception e){
            Logger.error(e);
            SystemError.wrapBs("500","解绑失败...");
        }
        return msgInfo;
    }

    public static MsgInfo pay(PayOrder payOrder,String desc,int type,String sign_no){
        MsgInfo msgInfo = new MsgInfo();
        try {
            Map<String, Object> map = new HashMap<>();
            RequestParam requestParam = new RequestParam();
            requestParam.setMch_no(mch_no);// 商户编号
            requestParam.setMethod("fastPay.agreement.pay");// 固定方法名
            requestParam.setVersion("1.0");// 版本号
            requestParam.setRand_str(getUUID());// 随机字符串
            requestParam.setSign_type("2");// 签名类型
            requestParam.setSec_key(RSAUtil.encryptByPublicKey(publicKeyStr,publicKeySign));//加密密钥
            map.put("mch_order_no", payOrder.getOrderNo()); // 商户订单号
            map.put("order_amount", StringHandler.fenToYuan(payOrder.amount+"")); // 订单金额，金额保留两位小数
            map.put("mch_req_time", DateHandler.getDate(new Date(),"yyyy-MM-dd HH:mm:ss")); // 交易请求时间
            map.put("order_desc", desc); // 订单描述
            map.put("callback_url", Mobo360Config._notifyUrl+"/hjzf/callBack/payNotify"); // 异步地址
            if(type==1){
                map.put("sign_no",  AESUtil.Aes256Encode(sign_no,"1234567891234567")); // 签约银行卡号
            }else if(type==2){
                map.put("bank_card_no",  AESUtil.Aes256Encode(sign_no,"1234567891234567")); // 签约银行卡号
            }
            requestParam.setData(JsonUtil.toString(map));

            String signStr = getSortedString(requestParam);

            if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) {
                signStr =(RSAUtil.sign(signStr, privateKey, false));
            } else {
                signStr = "";
            }
            if(StringHandler.isEmpty(signStr))
                SystemError.wrapBs("500","支付签名错误");

            requestParam.setSign(signStr);
            // Map转json字符串
            String reqBodyJson = JSON.toJSONString(requestParam);
            String httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson);
            Logger.info(httpResponseJson);
            Map<String, Object> httpResponseMap = (Map<String, Object>) JSONObject.parse(httpResponseJson);
            if(StringHandler.isNotEmpty(httpResponseMap)) {
                msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("message"));
                String resp_code = StringHandler.getString(httpResponseMap.get("resp_code"));
                String biz_code = StringHandler.getString(httpResponseMap.get("biz_code"));
                if(resp_code.equals("SUCCESS") && biz_code.equals("JS000000")){
                    msgInfo.isSuccess = true;
                }else{
                    Map<String, Object> dataMap = (Map<String, Object>) httpResponseMap.get("data");
                    if(StringHandler.isNotEmpty(dataMap)) {
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("errorDesc"));
                    }else{
                        msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("biz_msg"));
                    }
                    msgInfo.isSuccess=false;
                    Logger.error("支付错误--"+msgInfo.errMsg);
                }
            }
        }catch (Exception e){
            Logger.error(e);
            SystemError.wrapBs("500","解绑失败...");
        }
        return msgInfo;
    }
    public static MsgInfo paySms(PayOrder payOrder, String desc,int type,String sign_no){
        MsgInfo msgInfo = new MsgInfo();
        try {
            Map<String, Object> map = new HashMap<>();
            RequestParam requestParam = new RequestParam();
            requestParam.setMch_no(mch_no);// 商户编号
            requestParam.setMethod("fastPay.agreement.paySms");// 固定方法名
            requestParam.setVersion("1.0");// 版本号
            requestParam.setRand_str(getUUID());// 随机字符串
            requestParam.setSign_type("2");// 签名类型
            requestParam.setSec_key(RSAUtil.encryptByPublicKey(publicKeyStr,publicKeySign));//加密密钥
            map.put("mch_order_no", payOrder.getOrderNo()); // 商户订单号
            map.put("order_amount", StringHandler.fenToYuan(payOrder.amount+"")); // 订单金额，金额保留两位小数
            map.put("mch_req_time", DateHandler.getDate(new Date(),"yyyy-MM-dd HH:mm:ss")); // 交易请求时间
            map.put("order_desc", desc); // 订单描述
            map.put("callback_url", Mobo360Config._notifyUrl+"/hjzf/callBack/payNotify"); // 异步地址
            if(type==1){
                map.put("sign_no",  AESUtil.Aes256Encode(sign_no,"1234567891234567")); // 签约银行卡号
            }else if(type==2){
                map.put("bank_card_no",  AESUtil.Aes256Encode(sign_no,"1234567891234567")); // 签约银行卡号
            }
            requestParam.setData(JsonUtil.toString(map));
            String signStr = getSortedString(requestParam);
            // 签名
            if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) {
                signStr =(RSAUtil.sign(signStr, privateKey, false));
            } else {
                signStr = "";
            }
            if(StringHandler.isEmpty(signStr))
                SystemError.wrapBs("500","支付签名错误");
            requestParam.setSign(signStr);

            // Map转json字符串
            String reqBodyJson = JSON.toJSONString(requestParam);
            String httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson);
            Logger.info(httpResponseJson);
            Map<String, Object> httpResponseMap = (Map<String, Object>) JSONObject.parse(httpResponseJson);
            if(StringHandler.isNotEmpty(httpResponseMap)) {
                msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("message"));
                String resp_code = StringHandler.getString(httpResponseMap.get("resp_code"));
                String biz_code = StringHandler.getString(httpResponseMap.get("biz_code"));
                if(resp_code.equals("SUCCESS") && biz_code.equals("JS000000")){
                    msgInfo.isSuccess = true;
                }else{
                    if(StringHandler.isNotEmpty(httpResponseMap.get("data"))) {
                        Map<String, Object> dataMap = (Map<String, Object>) httpResponseMap.get("data");
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("errorDesc"));
                    }else{
                        msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("biz_msg"));
                    }
                    msgInfo.isSuccess=false;
                    Logger.error("支付错误--"+msgInfo.errMsg);
                }
            }
        }catch (Exception e){
            Logger.error(e);
            SystemError.wrapBs("500","支付失败");
        }
        return msgInfo;
    }

    public static MsgInfo smsPay(String orderSn,String smsCode){
        MsgInfo msgInfo = new MsgInfo();
        try {
            Map<String, Object> map = new HashMap<>();
            RequestParam requestParam = new RequestParam();
            requestParam.setMch_no(mch_no);// 商户编号
            requestParam.setMethod("fastPay.agreement.smsPay");// 固定方法名
            requestParam.setVersion("1.0");// 版本号
            requestParam.setRand_str(getUUID());// 随机字符串
            requestParam.setSign_type("2");// 签名类型
            requestParam.setSec_key(RSAUtil.encryptByPublicKey(publicKeyStr,publicKeySign));//加密密钥
            map.put("mch_order_no", orderSn); // 商户订单号
            map.put("sms_code", smsCode); // 订单金额，金额保留两位小数
            map.put("mch_req_time", DateHandler.getDate(new Date(),"yyyy-MM-dd HH:mm:ss")); // 交易请求时间
            requestParam.setData(JsonUtil.toString(map));
            String signStr = getSortedString(requestParam);
            // 签名
            if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) {
                signStr =(RSAUtil.sign(signStr, privateKey, false));
            } else {
                signStr = "";
            }
            if(StringHandler.isEmpty(signStr))
                SystemError.wrapBs("500","支付签名错误");
            requestParam.setSign(signStr);

            // Map转json字符串
            String reqBodyJson = JSON.toJSONString(requestParam);
            String httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson);
            Logger.info(httpResponseJson);
            Map<String, Object> httpResponseMap = (Map<String, Object>) JSONObject.parse(httpResponseJson);
            if(StringHandler.isNotEmpty(httpResponseMap)) {
                msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("message"));
                String resp_code = StringHandler.getString(httpResponseMap.get("resp_code"));
                String biz_code = StringHandler.getString(httpResponseMap.get("biz_code"));
                if(resp_code.equals("SUCCESS") && biz_code.equals("JS000000")){
                    if(StringHandler.isNotEmpty(httpResponseMap.get("data"))) {
                        Map<String, Object> dataMap = JsonUtil.toBean(httpResponseMap.get("data")+"",Map.class);
                        if(dataMap.get("order_status").equals("P1000")){
                            msgInfo.isSuccess = true;
                        }else if(dataMap.get("order_status").equals("P3000")){
                            msgInfo.isSuccess = true;
                            msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("err_msg"));
                        }else{
                            msgInfo.isSuccess = false;
                            msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("err_msg"));
                        }
                    }

                }else{
                    if(StringHandler.isNotEmpty(httpResponseMap.get("data"))) {
                        Map<String, Object> dataMap = JsonUtil.toBean(httpResponseMap.get("data")+"",Map.class);
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("errorDesc"));
                    }else{
                        msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("biz_msg"));
                    }
                    msgInfo.isSuccess=false;
                    Logger.error("支付错误--"+msgInfo.errMsg);
                }
            }
        }catch (Exception e){
            Logger.error(e);
            SystemError.wrapBs("500","支付失败");
        }
        return msgInfo;
    }
    public static String getSortedString(Object obj) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        if(!(obj instanceof Map)) {
            Field[] fields = obj.getClass().getDeclaredFields();

            for (int i = 0; i < fields.length; i++) {
                Field filed = fields[i];
                String name = filed.getName();
                if (SignUtil.NOT_SIGN_PARAM.contains(name)) {// 不参与签名或验签的参数直接跳过
                    continue;
                }
                filed.setAccessible(true);
                map.put(name, String.valueOf(filed.get(obj)));
            }
        }else{
            map = (Map<String, String>) obj;
        }
        StringBuffer content = new StringBuffer();
        List<String> keys = new ArrayList(map.keySet());
        Collections.sort(keys); // 排序map
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            Object value = map.get(key);

            if (i != 0) {
                content.append(SignUtil.SIGN_SEPARATOR);
            }
            content.append(key).append(SignUtil.SIGN_EQUAL).append(value);
        }
        return content.toString();

    }

    public static MsgInfo trans(WithdrawRecordWait record){
        MsgInfo msgInfo = new MsgInfo();
        boolean b = false;
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("userNo", mch_no);// 商户编号
            map.put("productCode", "BANK_PAY_DAILY_ORDER");//产品类型
            map.put("requestTime", DateHandler.getDate(new Date(),"yyyy-MM-dd HH:mm:ss")); // 交易请求时间
            map.put("merchantOrderNo", record.orderNo);//商户订单号
            map.put("receiverAccountNoEnc", record.cardNo); // 收款账户号
            map.put("receiverNameEnc", record.userName); // 收款人
            map.put("receiverAccountType", "201");//账户类型
//            map.put("receiverBankChannelNo", ""); // 收款账户联行号
            map.put("paidAmount", StringHandler.fenToYuan(record.withdrawAmount+"")); //交易金额
            map.put("currency", "201");//币种
            map.put("isChecked", "202");//是否复核
            map.put("paidDesc", "商户货款/用户佣金");//代付说明
            map.put("paidUse", "211");//代付用途
            map.put("callbackUrl", Mobo360Config._notifyUrl+"/hjzf/callBack/transNotify");//商户通知地址

            String signStr = getRequestSign(map);
            // 签名
            String hmac =(RSAUtil.sign(signStr, privateKey, false));
            if(StringHandler.isEmpty(hmac))
                SystemError.wrapBs("500","支付签名错误");
            map.put("hmac", hmac);/** 签名数据 */
            // Map转json字符串
            String reqBodyJson = JSON.toJSONString(map);
            String httpResponseJson = HttpClientUtil
                    .sendHttpPost("https://www.joinpay.com/payment/pay/singlePay",reqBodyJson);
            b = doResponseInfo(httpResponseJson, privateKey);
            if(!b){
                Map<String, Object> httpResponseMap = (Map<String, Object>) JSONObject.parse(httpResponseJson);
                if(StringHandler.isNotEmpty(httpResponseMap)) {
                    msgInfo.errMsg = StringHandler.getString(httpResponseMap.get("message"));
                    Map<String, Object> dataMap = (Map<String, Object>) httpResponseMap.get("data");
                    if(StringHandler.isNotEmpty(dataMap)) {
                        msgInfo.errMsg = msgInfo.errMsg+"-"+StringHandler.getString(dataMap.get("errorDesc"));
                    }
                }
            }

        }catch (Exception e){
            Logger.error(e);
        }
        msgInfo.isSuccess = b;
        return msgInfo;
    }

    /**
     * 对单笔代付响应信息的处理
     *
     * @param httpResponseJson 响应信息json字符串
     * @param key 商户秘钥
     * @throws Exception 异常实体类
     */
    private static boolean doResponseInfo(String httpResponseJson, String key) throws Exception {

        // 响应信息map集合
        Map<String, Object> httpResponseMap = (Map<String, Object>) JSONObject.parse(httpResponseJson);
        // 业务数据map集合
        Map<String, Object> dataMap = (Map<String, Object>) httpResponseMap.get("data");
        dataMap.put("statusCode", httpResponseMap.get("statusCode"));
        dataMap.put("message", httpResponseMap.get("message"));
        String respHmac = (String) dataMap.get("hmac");
        // 请求签名串
        String reqSign = getResponseSign(dataMap);
        return RSAUtil.verify(reqSign, publicTransKeyStr, respHmac, false);
    }

    /**
     * 获取请求数据签名串信息
     * 必须按新代付接口文档请求参数信息顺序来进行字符串的拼接，详情请参考新代付接口文档请求报文
     *
     * @param params 请求数据参数
     * @return 返回请求签名串
     */
    public static String getRequestSign(Map<String, Object> params) {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(params.get("userNo")).append(params.get("productCode")).append(params.get("requestTime"))
                .append(params.get("merchantOrderNo")).append(params.get("receiverAccountNoEnc"))
                .append(params.get("receiverNameEnc")).append(params.get("receiverAccountType"))
//                .append(params.get("receiverBankChannelNo"))
                .append(params.get("paidAmount"))
                .append(params.get("currency")).append(params.get("isChecked")).append(params.get("paidDesc"))
                .append(params.get("paidUse")).append(params.get("callbackUrl"))
                //.append(params.get("firstProductCode"))
                ;
        return stringBuilder.toString();
    }

    /**
     * 获取响应数据签名串信息
     * 必须按新代付接口文档应答参数信息顺序来进行字符串的拼接，详情请参考新代付接口文档的应答报文
     *
     * @param params 响应数据参数
     * @return 返回响应签名串
     */
    public static String getResponseSign(Map<String, Object> params) {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(params.get("statusCode")).append(params.get("message")).append(params.get("errorCode"))
                .append(params.get("errorDesc")).append(params.get("userNo")).append(params.get("merchantOrderNo"));

        return stringBuilder.toString();
    }

    /**
     * 转账支付回调签名
     * 获取响应数据签名串信息
     * 必须按新代付接口文档应答参数信息顺序来进行字符串的拼接，详情请参考新代付接口文档的应答报文
     *
     * @param transNotify 响应数据参数
     * @return 返回响应签名串
     */
    public static String getTransResponseSign(TransNotify transNotify) {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(transNotify.status).append(transNotify.errorCode)
                .append(transNotify.errorCodeDesc).append(transNotify.userNo);
        if(StringHandler.isNotEmpty(transNotify.tradeMerchantNo)){
            stringBuilder.append(transNotify.tradeMerchantNo);
        }
        stringBuilder.append(transNotify.merchantOrderNo)
                .append(transNotify.platformSerialNo).append(transNotify.receiverAccountNoEnc)
                .append(transNotify.receiverNameEnc).append(transNotify.paidAmount)
                .append(transNotify.fee);
        return stringBuilder.toString();
    }

    public static String getUUID(){
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        return uuid;
    }
}
